package com.itboye.textileexpres.top;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.R.color;
import android.R.integer;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.alipay.a.a.a;
import com.itboye.textileexpes.beans.Category_TypeTwo;
import com.itboye.textileexpes.beans.ForresterBeans;
import com.itboye.textileexpes.presenter.MainCategoryPresenter;
import com.itboye.textileexpes.presenter.MainCategoryTypeTwoPresenter;
import com.itboye.textileexpes.repository.MainCategoryRepositoryTypeTwo;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseFragment;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textillexepres.db.PropSub;
import com.itboye.textillexepres.db.SearchDBHelper;

public class FragmentGoodsSearch extends BaseFragment implements
		OnClickListener, Observer {

	ExpandableListView mListView;
	MyExpandableListAdapter adapter;
	List<String> list = new ArrayList<String>();
	String fid;
	String text;
	int cateID;
	MainCategoryPresenter presenter;
	ForresterBeans beans = new ForresterBeans();
	protected List<ForresterBeans> arrayListForm = new ArrayList<ForresterBeans>();
	protected List<Category_TypeTwo> arrayList = new ArrayList<Category_TypeTwo>();
	protected List<PropSub> propSub = new ArrayList<PropSub>();
	LinearLayout type_two_lin;
	int type;
	MainCategoryTypeTwoPresenter twopresenter;
	List<List<Category_TypeTwo>> childList = new ArrayList<List<Category_TypeTwo>>();
	SQLiteDatabase database;
	SearchDBHelper dbHelper;

	public FragmentGoodsSearch(int cateID, int type) {
		// TODO Auto-generated constructor stub
		this.cateID = cateID;
		this.type = type;
	}

	public FragmentGoodsSearch() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.fragment_goods_search;
	}

	@Override
	public void onMyActivityCreated() {
		// TODO Auto-generated method stub
//		presenter = new MainCategoryPresenter(this);
//		presenter.getInfo(beans);
		twopresenter = new MainCategoryTypeTwoPresenter(this);
		twopresenter.queryCate(cateID);
		mListView.setGroupIndicator(null);
	}

	OnClickListener totopclicker = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			int position = (Integer) v.getTag();
			if (position == 0) {
				toast("已经置顶了");
			} else {
				String str = arrayList.get(position).getName();
				for (int i = position; i > 0; i--) {
					String s = list.get(i - 1);
					list.remove(i);
					list.add(i, s);
				}
				list.remove(0);
				list.add(0, str);
				adapter.notifyAll();
			}
		}
	};

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		// case R.id.type_tv:
		// type = 0;
		// adapter = new GoodsFragmentAdapter2(getActivity(), type);
		// mListView.setAdapter(adapter);
		// adapter.notifyDataSetChanged();
		// break;
		// case R.id.technology_tv:
		// type = 1;
		// adapter = new GoodsFragmentAdapter2(getActivity(), type);
		// mListView.setAdapter(adapter);
		// adapter.notifyDataSetChanged();
		// break;
		// case R.id.component_tv:
		// type = 2;
		// adapter = new GoodsFragmentAdapter2(getActivity(), type);
		// mListView.setAdapter(adapter);
		// adapter.notifyDataSetChanged();
		// break;
		// case R.id.use_tv:
		// type = 1;
		// adapter = new GoodsFragmentAdapter2(getActivity(), type);
		// mListView.setAdapter(adapter);
		// adapter.notifyDataSetChanged();
		// break;
		}

	}

	@SuppressLint("ResourceAsColor")
	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		final ResultEntity entity = handlerError(data);
		if (entity != null) {
			propSub.clear();
			dbHelper = new SearchDBHelper(getActivity());
			database = dbHelper.getWritableDatabase();
			Cursor cursor = database.rawQuery("SELECT * FROM "
					+ SearchDBHelper.SEARCHDB_TABLENAME + " where type=" + type
					+ "", null);
			while (cursor.moveToNext()) {
				PropSub prop = new PropSub();
				prop.propId = cursor
						.getString(cursor.getColumnIndex("prop_id"));
				prop.propName = cursor.getString(cursor
						.getColumnIndex("prop_name"));
				propSub.add(prop);
			}
			for (int j = 0; j < propSub.size(); j++) {
				View view = LayoutInflater.from(getActivity()).inflate(
						R.layout.item_level_2, null);
				TextView textView = (TextView) view
						.findViewById(R.id.level2_tv);
				textView.setText(propSub.get(j).getPropName() + "");
				textView.setTag(j);
				textView.setTextColor(Color.parseColor("#000000"));
				android.widget.LinearLayout.LayoutParams params = new android.widget.LinearLayout.LayoutParams(
						android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,
						android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
				params.setMargins(2, 2, 2, 2);
				type_two_lin.addView(view, params);
				textView.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						// arrayList.clear();
						// twopresenter.queryCate(Integer.parseInt(propSub
						// .get(Integer.parseInt(v.getTag() + ""))
						// .getPropId()
						// + ""));
						toast("不知道什么功能");
					}
				});
				// }
			}
			if (entity.getEventType() == MainCategoryTypeTwoPresenter.Category2_success) {
				arrayList.addAll((List<Category_TypeTwo>) entity.getData());
				adapter = new MyExpandableListAdapter(getActivity(), arrayList,
						totopclicker);
				mListView.setAdapter(adapter);
				// for (int i = 0; i < adapter.getGroupCount(); i++) {
				//
				// mListView.expandGroup(i);
				//
				// }

				// mListView.setOnItemClickListener(new OnItemClickListener() {
				//
				// @Override
				// public void onItemClick(AdapterView<?> arg0, View arg1,
				// int arg2, long arg3) {
				// // TODO Auto-generated method stub
				// Intent intent = new Intent(getActivity(),
				// ActivityTypeDetail.class);
				// startActivity(intent);
				// }
				// });
				// mListView.setOnGroupClickListener(new OnGroupClickListener()
				// {
				//
				// @Override
				// public boolean onGroupClick(ExpandableListView parent,
				// View v, int groupPosition, long id) {
				// // TODO Auto-generated method stub
				// twopresenter.queryCate(Integer.parseInt(arrayList.get(
				// groupPosition).getId()));
				// return false;
				// }
				// });
				mListView.setOnChildClickListener(new OnChildClickListener() {

					@Override
					public boolean onChildClick(ExpandableListView parent,
							View v, int groupPosition, int childPosition,
							long id) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(getActivity(),
								GoodsDetailsActivity.class);
						intent.putExtra("pid", arrayList.get(childPosition).getId());
						startActivity(intent);
						return false;
					}
				});
			}
		}
	}
}
